const Subscription = require('egg').Subscription;

const { createData3 } = require('../utils/tools')


class Copy extends Subscription{
    static get schedule(){
        return {
            interval: '24h',
            type: 'worker',
            // immediate: true,
            disable: true
        }
    }

    // 跑一次就行了
    async subscribe(){
        let { ctx } = this;
        const { Op } = ctx.model.Sequelize
        let data = await ctx.model.TotalEnter.findAll({
            where: { type: 2, task_id: { [Op.not]: null } }
        })
        for(let i=0;i<data.length;i++){
            let enter_id = data[i].dataValues.id
            let detail = await ctx.model.TotalEnterDetail.findAll({ where: { enter_id } })
            let amounts = detail.map(item=>{
                let { amount, input } = item.dataValues
                return { amount, input }
            })
            createData3(amounts, enter_id, this)
        }
    }
}

module.exports = Copy